package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据查询条件分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<Brand> queryBrandsByPage(String key, Integer page, Integer rows,
                                               String sortBy, Boolean desc){

        //初始化example对象
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        //根据name模糊查询,或者根据首字母查询
        if(StringUtils.isNotBlank(key)){
            criteria.andLike("name","%" + key + "%").orEqualTo("letter",key);

        }

        //添加分页条件
        PageHelper.startPage(page,rows);

        //添加排序条件
        if(StringUtils.isNotBlank(sortBy)){
            example.setOrderByClause(sortBy+" " +(desc ? "desc" : "asc"));
        }
        List<Brand> brands = this.brandMapper.selectByExample(example);

        //包装成pageInfo
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);

        //包装成分页结果集返回
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());

    }

    /**
     * 新增产品
     * @param brand
     * @param cids
     */
    @Transactional  //事务注解,
    public void saveBrands(Brand brand, List<Long> cids) {

        //新增品牌,insertSelective 没有上传的字段会自动去掉
        this.brandMapper.insertSelective(brand);

        //新增中间表
        for (int i = 0; i < cids.size(); i++) {
            this.brandMapper.insertCategoryAndBrand(cids.get(i),brand.getId());
        }
        
    }

    /**
     * 修改品牌
     * @param brand
     */
    public void updateBrands(Brand brand,List<Long> cids) {

        //修改品牌
        this.brandMapper.updateByPrimaryKeySelective(brand);

        //修改中间表
        for (int i = 0; i < cids.size(); i++) {
            this.brandMapper.updateCategoryAndBrand(cids.get(i),brand.getId());
        }
    }

    /**
     * 删除品牌
     * @param bid
     */
    public void deleteBrands(Long bid) {
        //删除品牌
        this.brandMapper.deleteByPrimaryKey(bid);

        //删除中间表
        this.brandMapper.deleteCategoryAndBrand(bid);
    }

    /**
     * 根据cid查询品牌
     * @param cid
     * @return
     */
    public List<Brand> queryBrandByCid(Long cid) {

       return this.brandMapper.queryBrandByCid(cid);

    }

    /**
     * 根据品牌id查询品牌
     * @param id
     * @return
     */
    public Brand queryBrandById(Long id) {
        return this.brandMapper.selectByPrimaryKey(id);
    }
}
